<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="gurps.Character" %>
<%@ page import="guestbook.PMF" %>

<html>
  <body>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with characters you post.</p>
<%
    }
%>

<%
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String query = "select from " + Character.class.getName();
    List<Character> characters = (List<Character>) pm.newQuery(query).execute();
    if (characters.isEmpty()) 
	{
%>
<p>The character repository is empty.</p>
<%
    } 
	else 
	{
%>
<table border="1">
	<tr>
		<th>Author</th>
		<th>ID</th>
		<th>Name</th>
		<th>ST</th>
		<th>DX</th>
		<th>IQ</th>
		<th>HT</th>
		<th>Description</th>
	</tr>
<%
        for (Character c : characters) 
		{
%><tr><%
            if (c.getAuthor() == null) 
			{
%>
	<td>NA</td>
<%
            } 
			else 
			{
%>
	<td><%= c.getAuthor().getNickname() %></td>
<%
            }
%>
	<td><%= c.getId() %></td>
	<td><%= c.getName() %></td>
	<td><%= c.getStrength() %></td>
	<td><%= c.getDexterity() %></td>
	<td><%= c.getIntelligence() %></td>
	<td><%= c.getHealth() %></td>
	<td><%= c.getDescription() %></td>
<%
        }
    }
    pm.close();
%>
</table>
    <form action="/addCharacter" method="post">
      <div><input type="text" name="name" /></div>
      <div><textarea name="description" rows="3" cols="60"></textarea></div>
      <div><input type="text" name="strength" /></div>
      <div><input type="text" name="dexterity" /></div>
      <div><input type="text" name="intelligence" /></div>
      <div><input type="text" name="health" /></div>
      <div><input type="submit" value="Add character" /></div>
    </form>

  </body>
</html>